<?php
namespace app\store\controller\teacher;

use app\store\controller\Base;
use app\store\controller\Weixin;
use think\Db;
use think\Request;
use think\session;

class Article extends Base
{
    /**
     * yang yang
     * 发布文章
     * @return mixed
     */
//    public function send_article(Request $request){
//        if($request->isAjax()){
//            $data['sort'] = input("sort");
//            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
//            $data['status'] = 0;
//            $data['sort_id'] =  $status['id'];
//            $data['sort_status'] = $status['status'];
//            $data['title'] = input("title");
//            $data['edit']    = 0;
//            $data['author']    = input("author");
//            $data['content']    = input("content");
//            $data['create_time'] = time();
//            $data['teacher_id'] = $this->uid;
//            $ret =Db::name('article')->insert($data);
//            if($ret){
//                $this->success("提交成功");
//            }else{
//                $this->error("提交失败");
//            }
//        }
//        $teacher=Db::name('teacher')->where('teacher_id',$this->uid)->field('teacher_name')->find();
//        $sort = Db::name('article_sort')->where('is_del',0)->select();
//        $this->assign('sort',$sort);
//        $this->assign('author',$teacher['teacher_name']);
//        return $this->fetch();
//    }
    public function send_article(Request $request){
        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);

        $rr = $request->domain();
//        $url=$rr.'/store/teacher.article/wincode';
        $url=$rr.'/wincode';
        $this->assign('url', $url);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch();
    }

    /**
     * yang yang
     * 二维码页
     * @param Request $request
     * @return mixed
     */
    public function wincode(Request $request){

        $pc_token=md5(time());
        $rr = $request->domain();
        $url=$rr.'/'.$pc_token;
        $this->assign('pc_token',$pc_token);
        $this->assign('url',$url);
        return $this->fetch();
    }

    /**
     * yang yang
     * 手机端扫描token插入数据库
     * @param Request $request
     */
    public function inset_code(Request $request){
        if($request->isAjax()){
            $data = $request->param('data');
             $pc_token=substr ($data, -32);
            $up=Db::name('teacher')->where('teacher_id',$this->uid)->update(['pc_token'=>$pc_token]);
            if($up){
                $this->success('success');
            }
            else{
                $this->error('二维码有误');
            }

        }
    }

    /**yang yang
     * 电脑端二维码验证
     * @param Request $request
     * @return \think\response\Json
     */
    public function check_code(Request $request){
        if($request->isAjax()){
            $pc_token=$request->param('pc_token');
            $check=Db::name('teacher')->where('pc_token',$pc_token)->find();
            if($check){
                $teacher=$check['teacher_id'];
                session::set("teacher_id", $teacher);
                $this->success('success');
            }
        }
    }


    /**
     * yang yang
     * 电脑端添加文章
     * @return mixed
     */
    public function add_article(Request $request)
    {
        if(request()->isPost()){

            $model = Db::name('article');
            // 存入数据库
            $data['sort']      = input("sort");
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            $data['status'] = 0;
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];
            $data['title']    = input("title");
            $data['teacher_id']    = input("teacher_id");
            $data['edit']    = 0;
            $data['author']    = input("author");
            $data['content']    = input("content");
            $data['create_time'] = time();
            $ret =$model->insert($data);
            if($ret){
                $this->success("提交成功，等待审核");
            }else{
                $this->error("网络异常");
            }

        }
            $teacher_id=session::get('teacher_id');
            if($teacher_id==''){
                return $this->redirect('/store/teacher.article/wincode');
            }
            $author=Db::name('teacher')->where('teacher_id',$teacher_id)->field('teacher_name')->find();
            $sort = Db::name('article_sort')->where('is_del',0)->select();
            $this->assign('sort',$sort);
            $this->assign('author',$author['teacher_name']);
            $this->assign('teacher_id',$teacher_id);
            $this->assign('shop_id',$this->shop_id);
            return $this->fetch('add_article');

    }

    /**
     * yang yang
     * 电脑端退出文章编辑
     * @param Request $request
     */
    public function out(Request $request){
        if($request->isAjax()){
            $teacher_id=$request->param('teacher_id');
            $up=Db::name('teacher')->where('teacher_id',$teacher_id)->update(['pc_token'=>'']);
            session(null);
            if($up){
                $this->success('退出成功');
            }else{
                $this->error('退出失败');
            }
        }
    }
    /**
     * yang yang
     * 文章列表
     */
    public function list_article(){
        return $this->fetch();
    }

    /**
     * yang yang
     * 加载数据
     * @throws
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function loaddata(Request $request){
       if($request->isAjax()){
           $page = input('page') ? input('page') : 1;
           $limit = input('limit') ? input('limit') : 5;
           $teacher=Db::name('article')->where('teacher_id',$this->uid)->where('is_del',0)->order('id','desc')->page($page, $limit)->select();
           $sort=Db::name('article_sort')->where('is_del',0);
           if($teacher){
               foreach ($teacher as $index => $re){
                 $picture=$sort->where('id',$re['sort_id'])->field('picture')->find();
                   if (!$picture) {
                       $picture['picture'] =  '无';
                   }
                   $teacher[$index]['picture'] = $picture['picture'];
               }
               return json(['code' => 1, 'msg' => 'ok', 'count' => count($teacher), 'data' => $teacher]);
           }
       }
    }

    /**
     * yang yang
     * 删除文章
     * @param Request $request
     */
    public function del(Request $request){
        if($request->isAjax()){
            $id=$request->param('id');
            $res=Db::name('article')->where('id',$id)->update(['is_del'=>1]);
            if($res){
               $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }

        }
    }

    /**
     * yang yang
     * 文章编辑
     * @param Request $request
     * @return mixed
     */
    public function edit_article(Request $request){
        $id=$request->param('id');

        if($request->isPost()){
            $data['id'] = input("id");
            $data['sort'] = input("sort");
            $data['title'] = input("title");
            $data['edit']    = 1;
            $data['author'] = input("author");
            $data['content'] = input("content");
            $data['status'] = 0 ;
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];
            if(count($data)<=0){
                $this->error("未做任何修改");
            }

            $re = Db::name('article')->where('id',$data['id'])->update($data);
            if($re){
                $this->success("编辑成功");
            }else{
                $this->error("网络异常");
            }
        }
        $article=Db::name('article')->where('id',$id)->find();
        $this->assign('article',$article);
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);
        return $this->fetch();

    }

    /**
     * yang yang
     * 电脑端文章列表
     * @return mixed
     */
    public function art_list()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];
        $teacher_id=session::get('teacher_id');
        if($teacher_id==''){
            return $this->redirect('/store/teacher.article/wincode');
        }
        // 查询数据
        $list = Db::name('article')->where('teacher_id',$teacher_id)->where('is_del',0)->order('id','desc')->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);
        $this->assign('teacher_id',$teacher_id);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch();
    }
    /**
     * yang yang
     * @return mixed
     * 电脑端编辑文章
     */
    public function art_edit()
    {
        $id = input('id');

        $model = Db::name('article');;

        // 更新数据
        if(request()->isPost()){

            $data['sort'] = input("sort");
            $data['title'] = input("title");
            $data['edit']    = 1;
            $data['author'] = input("author");
            $data['content'] = input("content");
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            if($status['status']==1){
                $data['status'] = 0;
            }
            if($status['status']==0){
                $data['status'] = 1;
            }
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];

            if(count($data)<=0){
                $this->error("未做任何修改");
            }

            $re = $model->where('id',$id)->update($data);
            if($re){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }
        }

        // 详情
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);
        $this->assign('shop_id',$this->shop_id);

        return $this->fetch();
    }
    /**
     * yang yang
     * 电脑端删除文章
     */
    public function art_del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('article')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

}
